﻿import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Window 2.12

/*
    提供拖拽能力和双击事件的标题栏，颜色等参数由调用方提供
*/
Rectangle {
    property Window windowRef: undefined;   // @require
    property alias plRectangle: m_titleRect.sourceComponent // @require
    property alias getDoubleClickedMouse: m_private.doubleClickedMouse // @require

    signal sgTitleDoubleClick;

    QtObject {
        id: m_private
        property bool isClick: false
        property int rwindowRefWidth: 0
        property point doubleClickedMouse: "0,0"
    }

    id: m_titleBar

    MouseArea {
        anchors.fill: parent
        property point clickPos: "0,0"

        onPressed: {
            m_private.isClick = true;
            m_private.rwindowRefWidth=windowRef.width;
            clickPos = Qt.point(mouse.x,mouse.y)
        }

        onPositionChanged: {
            if (!m_private.isClick)
                return;


            if(Qt.platform.os==="windows")
            {
                windowRef.visibility=Window.Windowed
            }
            if(pressed){
                var x = (m_private.rwindowRefWidth-windowRef.width)*mouse.x/windowRef.width+mouse.x-clickPos.x+windowRef.x;
                var y = mouse.y-clickPos.y+windowRef.y;
                windowRef.x = x;
                windowRef.y = y;
            }
        }
        onReleased:{

        }

        onDoubleClicked: {
            m_private.isClick = false;
            m_private.doubleClickedMouse=Qt.point(mouse.x+windowRef.x,mouse.y+windowRef.y);
            sgTitleDoubleClick();
        }
    }

    Loader {
        id:m_titleRect
        anchors.fill: parent

        sourceComponent: undefined
    }
}

